package com.iwhalecloud.citybrain.flow.platform.manager.application.utils;

import java.util.HashMap;
import java.util.Map;

import org.springframework.cglib.beans.BeanCopier;

/**
 * @author lilh
 * @date 2021/2/26 14:33
 */
public class BeanCopierUtils {

    private static Map<String, BeanCopier> beanCopierCacheMap = new HashMap<>(16);

    public static void copyProperties(Object source, Object target) {
        String cacheKey = source.getClass().getName() + target.getClass().getName();
        if (!beanCopierCacheMap.containsKey(cacheKey)) {
            synchronized (beanCopierCacheMap) {
                if (!beanCopierCacheMap.containsKey(cacheKey)) {
                    BeanCopier beanCopier = BeanCopier.create(source.getClass(), target.getClass(), false);
                    beanCopierCacheMap.put(cacheKey, beanCopier);
                }
            }
        }
        beanCopierCacheMap.get(cacheKey).copy(source, target, null);
    }
}
